package com.fengxg.test.recursive.action;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.RecursiveAction;

/**
 * @Desccription 分片后执行业务任务的具体类
 * @auther Fengxg
 * @create 2018/7/25
 */
public class ActionWorkTask  extends RecursiveAction{

    public ActionWorkTask(int startIndex, int endIndex, CountDownLatch countDownLatch, String[] datas, int workNum) {
        super();
        this.startIndex = startIndex;
        this.endIndex = endIndex;
        this.datas = datas;
        this.workNum = workNum;
        this.countDownLatch = countDownLatch;
    }

    private int workNum;
    private int startIndex;
    private int endIndex;
    private String[] datas;
    private CountDownLatch countDownLatch;

    @Override
    protected void compute() {
        Thread t = Thread.currentThread();
        for (int i = startIndex; i < endIndex; i++) {
            try {
                Thread.sleep(100L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("workNum" + workNum  +"，第" + datas[i] + "任务," + "执行线程:" + t.getName());
        }
        countDownLatch.countDown();
    }

    public int getStartIndex() {
        return startIndex;
    }

    public void setStartIndex(int startIndex) {
        this.startIndex = startIndex;
    }

    public int getEndIndex() {
        return endIndex;
    }

    public void setEndIndex(int endIndex) {
        this.endIndex = endIndex;
    }

    public String[] getDatas() {
        return datas;
    }

    public void setDatas(String[] datas) {
        this.datas = datas;
    }
}
